package com.molin.networkTest.socketTest.tcpTest.Demo4;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class CopyfileRunnable implements Runnable{
    Socket accept;
    public CopyfileRunnable(Socket socket) {
        this.accept=socket;
    }

    @Override
    public void run() {
        BufferedInputStream bi=null;
        BufferedOutputStream bo=null;
        try {

            //网络流，读到内存
            bi=new BufferedInputStream(accept.getInputStream());
            //内存到磁盘
            String dir="D:\\IdeaProjects\\BasicCode\\mIdeaTest\\src\\com\\molin\\networkTest\\socketTest\\tcpTest\\Demo4\\";
            String filename= UUID.randomUUID().toString().replace("-","");
            String path=dir+filename+".jpg";
            bo=new BufferedOutputStream(new FileOutputStream(path));

            int b;
            while ((b=bi.read())!=-1){
                bo.write(b);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bo!=null){
                try {
                    bo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(bi!=null){
                try {
                    bi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
